//
//  HD_NKM_LanguageVC.m
//  HDLiaoNingMeseum
//
//  Created by HDNiuKuiming on 2017/8/3.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HD_NKM_LanguageVC.h"
#import "HD_NKM_LanguageVC+NetTip.h"
#import "SSZipArchive.h"
#import "SCHCircleView.h"
#import "viewCell.h"
#import "HD_LY_NetHelper.h"

@interface HD_NKM_LanguageVC ()<SCHCircleViewDataSource,SCHCircleViewDelegate>
{
    HDDeclare *declare;
}
@property (nonatomic, strong) SCHCircleView *circle_view;
@property (strong, nonatomic) NSMutableArray  *icon_array;
@property (strong, nonatomic) NSArray *imgArray;

@end

@implementation HD_NKM_LanguageVC

#pragma mark
#pragma mark -------- First Launch Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    declare = [HDDeclare sharedDeclare];
    
    [declare giveMeExhibitData];
    
    NSLog(@"资源的位置：%@",declare.catch_path);
    
//    [self testMethod];添加系统通知
    
    [self datasInit];
    [self firstLaunchInit];
    
    [self getMachainNum];
    [self getDateBaseDate];
    
    [self connectTCP];//链接tcp
}

-(void)getDateBaseDate
{
    NSString *requestUrl = [NSString stringWithFormat:@"%@%@",[HDDeclare IP_Request_Header],@"index.php?g=Mapi&m=Resource&a=db_resource"];
    [HD_LY_NetHelper requestWithType:POST withUrl:requestUrl withParams:nil toShowVC:self success:^(id response) {
        NSDictionary *dicData = response;
        if ([dicData[@"status"] intValue] == 1)
        {
            NSDictionary *dic = [dicData objectForKey:@"data"];
            NSArray *arr = [dic objectForKey:declare.language.uppercaseString];
            declare.resArray = arr;
        }else {
            
        }
    } fail:^(NSError *error) {
        
    } showHUD:NO isCache:YES];
}

-(void)getMachainNum
{
    NSString *tempStr = [declare getUserPhoneID];
    if (tempStr) {
        declare.userID = tempStr;
    }
    else
    {
        NSString *requestUrl = [NSString stringWithFormat:@"%@%@",[HDDeclare IP_Request_Header],@"index.php?g=Mapi&m=User&a=generate_device_id"];
        NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",(long)declare.languageIndex],@"language",@"2",@"app_kind", nil];
        [HD_LY_NetHelper requestWithType:POST withUrl:requestUrl withParams:params toShowVC:self success:^(id response) {
            NSDictionary *dicData = response;
            if ([dicData[@"status"] intValue] == 1)
            {
                NSDictionary *dic = [dicData objectForKey:@"data"];
                [[NSUserDefaults standardUserDefaults] setObject:[dic objectForKey:@"device_id"] forKey:kFirst_Machine_ID];
            }else {
                
            }
        } fail:^(NSError *error) {
            
        } showHUD:NO isCache:NO];
    }
}
- (void)connectTCP{
    declare = [HDDeclare sharedDeclare];
//    if (declare.loginStatus==kLogin_Status_Login) {
        //tcp://192.168.10.158:8282
        [SSLSocketManager sharedInstance].socketHost = TCP_SERVER;
        [SSLSocketManager sharedInstance].socketPort = TCP_PORT;
        [[SSLSocketManager sharedInstance] tcpSocketCutOff];
        [[SSLSocketManager sharedInstance] tcpSocketConnect];
//    }
}
//-(void)testMethod
//{
//    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
//    NSString *fileName = [path stringByAppendingPathComponent:@ "123.plist" ];
//    
//    NSArray *array = @[@ "123" , @ "456" , @ "789" ];[array writeToFile:fileName atomically:YES];//执行此行代码时默认新创建一个plist文件
//    
//    
//}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.imgArray = @[@"language_C",
                      @"language_E",
                      @"language_J",
                      @"language_K"];
    
    self.circle_view.hidden = NO;
}

- (void)firstLaunchInit
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"first_launch"] == nil)
    {
        NSString *resource = [[NSBundle mainBundle]pathForResource:@"builtIn" ofType:@"zip"];
        
        MBProgressHUD *progressHUD = [[MBProgressHUD alloc]initWithView:self.navigationController.view];
        [self.navigationController.view addSubview:progressHUD];
        progressHUD.labelText =  @"正在安装...";
        progressHUD.labelColor = Common_Color;
        [progressHUD showAnimated:YES whileExecutingBlock:^{
            NSString *directoryPath = declare.catch_path;
            [SSZipArchive unzipFileAtPath:resource toDestination:directoryPath progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
                
            } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
                
            }];
        } completionBlock:^{
            [[NSUserDefaults standardUserDefaults]setObject:@"first_launch" forKey:@"first_launch"];
            declare.language = kLanuage_CHINESE;
            [self netStatusAction];
        }];
    }
    else {
        [self netStatusAction];
    }
    
    [self rotateViewInit]; // 初始化旋转view
}

- (void)rotateViewInit
{
    NSInteger languageKind = 4;
    if (_imgArray.count < languageKind)
        return;
    
    _icon_array = [[NSMutableArray alloc] init];
    for (NSInteger i=0; i<languageKind; i++) {
        [_icon_array addObject:[UIImage imageNamed:self.imgArray[i]]];
    }
    
    _circle_view = [[SCHCircleView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-300-150, self.view.frame.size.width, 350)];
    _circle_view.backgroundColor = [UIColor clearColor];
    _circle_view.circle_view_data_source = self;
    _circle_view.circle_view_delegate    = self;
    _circle_view.show_circle_style       = SChShowCircleWinding;
    [_circle_view reloadData];
    
    [self.view addSubview:_circle_view];
}

- (void)datasInit
{
    self.imgArray = @[@"language_C",
                      @"language_E",
                      @"language_J",
                      @"language_K"];
}



#pragma mark
#pragma mark -------- Language Methods --------
#define Language_C 101
#define Language_E 102
#define Language_J 103
#define Language_K 104

- (IBAction)choiceAction:(NSInteger)selectedTag
{
    NSInteger btnIndex = selectedTag+Language_C;
    Language_Type selectLanguage = kLanguage_Type_Chinese;
    
    switch (btnIndex) {
        case Language_C:
            selectLanguage = kLanguage_Type_Chinese;
            break;
            
        case Language_E:
            selectLanguage = kLanguage_Type_English;
            break;
            
        case Language_J:
            selectLanguage = kLanguage_Type_Japanese;
            break;
            
        case Language_K:
            selectLanguage = kLanguage_Type_Korean;
            break;
            
        default:
            break;
    }
    [[HDDeclare sharedDeclare] setLanguageType:selectLanguage];
    
    [declare judgeIsResourceAll];
    [self performSegueWithIdentifier:languagePushToRootVCLine sender:nil];
}

#pragma mark
#pragma mark -------- SCHCircleViewDataSource Methods --------

- (IBAction)pressed:(id)sender
{
    [_circle_view reloadData];
}

- (CGPoint)centerOfCircleView:(SCHCircleView *)circle_view
{
    CGFloat centerY = 165.0f;
    if (self.view.frame.size.height == 480)
        centerY = 200;
    
    return CGPointMake(self.view.frame.size.width/2, centerY);
}

- (CGFloat)radiusOfCircleView:(SCHCircleView *)circle_view;
{
    CGFloat scaleTemp = 3.5;
    return self.view.frame.size.width/scaleTemp;
}

- (CGFloat)scaleOfCircleViewCell:(SCHCircleView *)circle_view
{
    return 0.85f;
}

- (NSInteger)numberOfCellInCircleView:(SCHCircleView *)circle_view
{
    return _icon_array.count;
}

- (SCHCircleViewCell *)circleView:(SCHCircleView *)circle_view cellAtIndex:(NSInteger)index_circle_cell
{
    viewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"viewCell" owner:nil options:nil] lastObject];
    
    [cell.image_view setImage:[_icon_array objectAtIndex:index_circle_cell]];
    
    if (index_circle_cell == 0)
    {
        NSString *imgName = [NSString stringWithFormat:@"%@_P", _imgArray[index_circle_cell]];
        cell.image_view.image = [UIImage imageNamed:imgName];
    }
    cell.tag = index_circle_cell;
    
    return cell;
}

- (void)dragEndCircleViewCell:(SCHCircleViewCell *)cell indexOfCircleViewCell:(NSInteger)index;
{
    for (viewCell *cell in self.circle_view.subviews)
    {
        if ([cell isKindOfClass:[viewCell class]] && cell.tag != index)
        {
            NSString *imgName = [NSString stringWithFormat:@"%@", _imgArray[cell.tag]];
            cell.image_view.image = [UIImage imageNamed:imgName];
        }
        else if ([cell isKindOfClass:[viewCell class]] && cell.tag == index)
        {
            NSString *imgName = [NSString stringWithFormat:@"%@_P", _imgArray[index]];
            cell.image_view.image = [UIImage imageNamed:imgName];
        }
    }
}

- (void)touchBeginCircleViewCell:(SCHCircleViewCell *)cell indexOfCircleViewCell:(NSInteger)index;
{
    NSString *imgName = [NSString stringWithFormat:@"%@_P", _imgArray[index]];
    viewCell *cellTemp = (viewCell *)cell;
    cellTemp.image_view.image = [UIImage imageNamed:imgName];
    
    [self choiceAction:index];
    LOG(@"%ld",(long)index);
}

// 点击进入到下一个页面
- (void)touchEndCircleViewCell:(SCHCircleViewCell *)cell indexOfCircleViewCell:(NSInteger)index
{
    NSString *imgName = [NSString stringWithFormat:@"%@_P", _imgArray[index]];
    viewCell *cellTemp = (viewCell *)cell;
    cellTemp.image_view.image = [UIImage imageNamed:imgName];
    
    [self choiceAction:index];
    LOG(@"%ld",(long)index);
}

- (void)changeImageCircleViewCell:(SCHCircleViewCell *)cell indexOfCircleViewCell:(NSInteger)index
{
    index = index%4;
    for (viewCell *cell in self.circle_view.subviews)
    {
        if ([cell isKindOfClass:[viewCell class]] && cell.tag != index)
        {
            NSString *imgName = [NSString stringWithFormat:@"%@", _imgArray[cell.tag]];
            cell.image_view.image = [UIImage imageNamed:imgName];
        }
        else if ([cell isKindOfClass:[viewCell class]] && cell.tag == index)
        {
            NSString *imgName = [NSString stringWithFormat:@"%@_P", _imgArray[index]];
            cell.image_view.image = [UIImage imageNamed:imgName];
        }
    }
}

- (void)tapCircleViewCell:(SCHCircleViewCell *)cell indexOfCircleViewCell:(NSInteger)index
{
    for (viewCell *cell in self.circle_view.subviews) {
        if ([cell isKindOfClass:[viewCell class]] && cell.tag != index) {
            NSString *imgName = [NSString stringWithFormat:@"%@", _imgArray[cell.tag]];
            cell.image_view.image = [UIImage imageNamed:imgName];
        }
        else if ([cell isKindOfClass:[viewCell class]] && cell.tag == index) {
            NSString *imgName = [NSString stringWithFormat:@"%@_P", _imgArray[index]];
            cell.image_view.image = [UIImage imageNamed:imgName];
        }
    }
    
    NSString *imgName = [NSString stringWithFormat:@"%@_P", _imgArray[index]];
    viewCell *cellTemp = (viewCell *)cell;
    cellTemp.image_view.image = [UIImage imageNamed:imgName];
    
    [self choiceAction:index];
    LOG(@"%ld",(long)index);
}


#pragma mark
#pragma mark -------- Clean Memory --------

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.imgArray = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
